/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package comits.web.client.servlets.demo;
package comits.web.client.servlets.demo;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import comits.business.tiers.dao.LigneCmdDAO;
import comits.business.tiers.dao.LigneCmdDAOImpl;
import comits.business.tiers.metiers.Boutique;
import comits.business.tiers.metiers.Commande;
import comits.business.tiers.metiers.Depot;
import comits.business.tiers.metiers.LigneCmd;
import comits.business.tiers.requests.manager.ws.RequestsManager;
import comits.business.tiers.utilitaires.LoggingOutputStream;

/**
 * 
 * @author assia
 */
public class PlanningServlet extends HttpServlet {

	private static final long serialVersionUID = 7526928611471348888L;

	private  Date date;

	/*public void init() throws ServletException {
		date = Calendar.getInstance().getTime();
	}*/

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");


		if (action == null){
			date=Calendar.getInstance().getTime();
			request.setAttribute("date", date);
			listCommande(request,response,date);
			getServletContext().getRequestDispatcher("/Planning.jsp").forward(request, response);
		}else{
			if (action.equals("Suivant")) {
				date= DateUtil.nextWeek(date);
				request.setAttribute("date", date);
				Date monday = new Date(date.getTime());
				listCommande(request,response,monday);
				getServletContext().getRequestDispatcher("/Planning.jsp").forward(request, response);

			} else if (action.equals("Precedent")) {
				date = DateUtil.lastWeek(date);
				request.setAttribute("date", date);
				Date monday = new Date(date.getTime());
				listCommande(request,response,monday);
				getServletContext().getRequestDispatcher("/Planning.jsp").forward(request, response);

			} else if (action.equals("Retour")) {
				date=DateUtil.mondayOfWeek(date);
				request.setAttribute("date", date);
				Date monday = new Date(date.getTime());
				listCommande(request,response,monday);
				getServletContext().getRequestDispatcher("/Planning.jsp").forward(request, response);

			} else {
				StringTokenizer tokenizer = new StringTokenizer(action, " ");
				String NumCmd = tokenizer.nextToken();
				String datejour = tokenizer.nextToken();
				String expediteur = tokenizer.nextToken();
				String numexpediteur = tokenizer.nextToken();
				String destinataire = tokenizer.nextToken();
				String numdestinataire = tokenizer.nextToken();
				LoggingOutputStream log =  new LoggingOutputStream();
				log.initializeLog();
				
				HttpSession session = request.getSession(); 
				String exp = getNameFournisseur(expediteur,session,numexpediteur);
				String dest = getDestinataire(destinataire,session);
				
				if (exp.equalsIgnoreCase(" ")){
					
					exp=expediteur +"  "+ numexpediteur;
				}
				
				if (dest.equalsIgnoreCase(" ")){
					
					dest=destinataire +"  "+ numdestinataire;
				}
				request.setAttribute("NumCmd", NumCmd);
				request.setAttribute("datejour", datejour);
				request.setAttribute("expediteur", exp);
				request.setAttribute("nexpediteur", expediteur);
				request.setAttribute("destinataire", dest);
				request.setAttribute("ndestinataire", destinataire);
				
				System.out.println("detail encours :" + NumCmd + " " + datejour + " " + expediteur + " " + numexpediteur + " " + destinataire + " " + numdestinataire);
				LigneCmdDAO cmdAo = new LigneCmdDAOImpl();
				//ProduitDAO pdtDao = new ProduitDAOImpl();
				Date d = new Date(DateUtil.stringToDate(datejour).getTime());
				List<LigneCmd> queryresult1 =  cmdAo.getLigneCmdPlan(d, Integer.valueOf(NumCmd));
				session.setAttribute("queryresult1",queryresult1);

				
				getServletContext().getRequestDispatcher("/DetailEncours.jsp").forward(request, response);

			}
		}

	}



	public void listCommande (HttpServletRequest request,
			HttpServletResponse response, Date date) throws ServletException, IOException {

		HttpSession session = request.getSession();
		RequestsManager rM = new RequestsManager();
		List<Commande> queryresults=null;

		if (session.getAttribute("site").equals("Boutique")) {
			List<Boutique> listBtq = (List<Boutique>) session
			.getAttribute("listBtq");
			int index = ((Integer) session.getAttribute("idLog")) - 1;
			queryresults = rM.getCommandeDest(listBtq.get(index)
					.getNumBtq(), "Boutique", date);
			session.setAttribute("queryresults",queryresults);
		}

		if (session.getAttribute("site").equals("Depot")) {
			List<Depot> lstDpt = (List<Depot>) session
			.getAttribute("listDpt");
			int index = ((Integer) session.getAttribute("idLog")) - 1;
			queryresults = rM.getCommandeDest(
					lstDpt.get(index).getNumDpt(), "Depot", date);
			session.setAttribute("queryresults",queryresults);
		}
		if (session.getAttribute("site").equals("Logistique")) {
			queryresults = rM.getAllCommande(date);
			session.setAttribute("queryresults",queryresults);
		}

		}
	public String getDestinataire (String dest,HttpSession session){
		
		if (dest.equalsIgnoreCase("Boutique")) {
			List<Boutique> listBtq = (List<Boutique>) session
			.getAttribute("listBtq");
			int index = ((Integer) session.getAttribute("idLog")) - 1;
			return listBtq.get(index).getLibelleBtq();
		}

		if (dest.equalsIgnoreCase("Depot")) {
			List<Depot> lstDpt = (List<Depot>) session
			.getAttribute("listDpt");
			int index = ((Integer) session.getAttribute("idLog")) - 1;
			return lstDpt.get(index).getNom();
		}
		
		return " ";
	}
	
	
	public String getNameFournisseur(String fourn, HttpSession session, String numexpediteur){
		
		if (fourn.equalsIgnoreCase("Fournisseur")) {
			RequestsManager rm = new RequestsManager();
			String exp = rm.getNameFournisseur(Integer.valueOf(numexpediteur));
			return exp;
		}

		if (fourn.equalsIgnoreCase("Depot")) {
			List<Depot> lstDpt = (List<Depot>) session.getAttribute("listDpt");
			for (int i = 0; i < lstDpt.size(); i++){
				if(lstDpt.get(i).getNumDpt()==Integer.valueOf(numexpediteur)) return lstDpt.get(i).getNom();
			}
		}
		return " ";
	}
}
